package com.hui.dao.impl;

import com.hui.bean.RecordBean;
import com.hui.dao.IRecordDao;
import com.hui.dao.IRoomDao;
import com.hui.util.DBUtils;
import org.apache.ibatis.session.SqlSession;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class RecordDaoImpl  implements IRecordDao {
    @Override
    public List<RecordBean> getAllRecordsByRoomId(int roomId) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IRecordDao mapper = session.getMapper(IRecordDao.class);
        List<RecordBean> recordBeanList = mapper.getAllRecordsByRoomId(roomId);
        session.close();
        return recordBeanList;
      }

    @Override
    public int saveRecord(RecordBean recordBean, int roomId) throws SQLException {

        SqlSession session = DBUtils.getSqlSession();
        IRecordDao mapper = session.getMapper(IRecordDao.class);
        IRoomDao roomDao = session.getMapper(IRoomDao.class);
        int result = mapper.saveRecord(recordBean, roomId);
        roomDao.updateRoomStatusToError(roomId);
        session.commit();
        session.close();
        return result;

 }

    @Override
    public int updateRecord(int recordId, int roomId) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IRecordDao mapper = session.getMapper(IRecordDao.class);
        IRoomDao roomDao = session.getMapper(IRoomDao.class);

        int result = mapper.updateRecord(recordId, roomId);
        roomDao.updateRoomStatusToNormal(roomId);
        session.commit();
        session.close();
        return result;
      }

    @Override
    public int getRecordStatusIsZeroAccountByRoomId(int roomId) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IRecordDao mapper = session.getMapper(IRecordDao.class);
        int count = mapper.getRecordStatusIsZeroAccountByRoomId(roomId);
        session.close();
        return count;
   }


}
